package com.cz.nongyetongv5.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.TouRuPinIconGrideAdapter7;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.utils.GoHomeUtil;

public class ChanpinMenuActivity extends Activity implements OnClickListener {

	private ViewGroup btnBack = null;

	private GridView gvTouRuPinMenu = null;

	private User user = null;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.tourupin_menu);
		TextView title=(TextView)this.findViewById(R.id.tvTitle);
		if(title!=null)
			title.setText("样品抽检");
		initView();
		initDate();

		loadRightTitle();

	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());

				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	private void initView() {

		gvTouRuPinMenu = (GridView) findViewById(R.id.gvTouRuPinMenu);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		if(btnBack!=null)
			btnBack.setOnClickListener(this);

	}

	private void initDate() {

		user = (User) getIntent().getSerializableExtra("user");

		final List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();

		Map<String, Object> icon_1 = new HashMap<String, Object>();
		if (CommonConstants.isNetworkConnected(ChanpinMenuActivity.this)) {

			icon_1.put("ibIconItem", R.drawable.ic_cy_plan);
			icon_1.put("tvIconItem", "样品抽样");
			datas.add(icon_1);
		}

		Map<String, Object> icon_2 = new HashMap<String, Object>();
		icon_2.put("ibIconItem", R.drawable.ic_cy_yp);
		icon_2.put("tvIconItem", "抽样列表");
		datas.add(icon_2);
		if (CommonConstants.isNetworkConnected(ChanpinMenuActivity.this)) {
			Map<String, Object> icon_3 = new HashMap<String, Object>();
			icon_3.put("ibIconItem", R.drawable.ic_cy_jc);
			icon_3.put("tvIconItem", "抽检结果");
			datas.add(icon_3);
		}else{
			Toast.makeText(this, "未检测到网络,略过展示统计信息...",
					Toast.LENGTH_LONG).show();
		}

		TouRuPinIconGrideAdapter7 adapter = new TouRuPinIconGrideAdapter7(this,
				datas, getLayoutInflater(), new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				String position = String.valueOf(v.getTag());

				Intent intent=null;
				if (position.equals("抽检结果")) {
					intent = new Intent(ChanpinMenuActivity.this,
							ChanPinJianCeStatisticsActivity.class);
					intent.putExtra("title", "抽检结果");
					intent.putExtra("type", "3");

				} else if (position.equals("抽样列表")) {
					if (CommonConstants.isNetworkConnected(ChanpinMenuActivity.this)) {
						intent = new Intent(ChanpinMenuActivity.this,
								YangpinchouyangActivity.class);
					}else{
						Toast.makeText(ChanpinMenuActivity.this,"未检测到网络,略过展示统计信息...",
								Toast.LENGTH_LONG).show();
						intent = new Intent(ChanpinMenuActivity.this,ChanPinJianCeActivity.class);
						intent.putExtra("user", user);
						startActivity(intent);
					}
					intent.putExtra("title", "抽样列表");
					intent.putExtra("type", "2");
				}else if (position.equals("样品抽样")) {
					intent = new Intent(ChanpinMenuActivity.this,
							ChouyangrenwuActivity.class);
					intent.putExtra("title", "样品抽样");
					intent.putExtra("type", "1");
				}
				intent.putExtra("user", user);
				startActivity(intent);
			}
		});

		gvTouRuPinMenu.setAdapter(adapter);

	}

	@Override
	public void onClick(View v) {

		if (btnBack == v) {

			onBackPressed();
		}

	}
}
